using System;
using System.Collections.Generic;
using System.IO;

namespace guosange.Common
{
	/// <summary>
	/// Summary description for Class1.
	/// </summary>
	public class LogFile
	{
		String m_szLogPath;		// log file path
		int m_nExpireDays;		// how many days before log file delete, default = 1
		String m_szLogExt;		// log file extention
		String m_szFileName;	// log file name (fix name) instead of using date    

		long m_lSize;	

		public LogFile()
        {
            m_szLogPath = ".\\Log\\";
            if(!Directory.Exists(m_szLogPath))
            {
                Directory.CreateDirectory(m_szLogPath);
            }
			m_szLogExt = "log";
			m_szFileName = "";
			m_nExpireDays = 30;
			m_lSize = 1000000;
		}

		public LogFile(String szLogPath, String szFileName, String szLogExt, int nExpireDays, long lSize)
		{
			m_szLogPath = szLogPath;
			m_szFileName = szFileName;
			m_szLogExt = szLogExt;
			m_nExpireDays = nExpireDays;
			m_lSize = lSize;
		}

		public String Path
		{
			get
			{
				return m_szLogPath;
			}
			set
			{
				m_szLogPath = value;
			}
		}

		public String Extension
		{
			get
			{
				return m_szLogExt;
			}
			set
			{
				m_szLogExt = value;
			}
		}

		public String FileName
		{
			get
			{
				return m_szFileName;
			}
			set
			{
				m_szFileName = value;
			}
		}

		public int ExpireDays
		{
			get
			{
				return m_nExpireDays;
			}
			set
			{
				m_nExpireDays = value;
			}
		}

		public long LimitSize
		{
			get
			{
				return m_lSize;
			}
			set
			{
				m_lSize = value;
			}
		}

		// write to log file
		public void LogWrite(String szLogMsg)
		{
			try 
			{
				String szLogPathFile;
				DateTime curDT = DateTime.Now;

				//DeleteExpiredFile();
				String szCurrentFileName = m_szFileName;
				if (szCurrentFileName == "")
				{
                    szCurrentFileName = curDT.ToString("yyyy-MM-dd");
				}

				szLogPathFile = m_szLogPath + "\\" +
					szCurrentFileName + "." + m_szLogExt;

				//Pass the filepath and filename to the StreamWriter Constructor
				StreamWriter sw = new StreamWriter(szLogPathFile, true);

				//Write a line of text
				sw.WriteLine(curDT.ToString() + " " + szLogMsg);

				//Close the file
				sw.Close();
			}
			catch(Exception ex)
			{
				throw ex;
			}
		}


        // write to log file
        public void LogWrite(List<string> listLog)
        {
            try
            {
                String szLogPathFile;
                DateTime curDT = DateTime.Now;

                //DeleteExpiredFile();
                String szCurrentFileName = m_szFileName;
                if (szCurrentFileName == "")
                {
                    szCurrentFileName = curDT.ToString("yyyy-MM-dd");
                }

                szLogPathFile = m_szLogPath + "\\" +
                    szCurrentFileName + "." + m_szLogExt;

                //Pass the filepath and filename to the StreamWriter Constructor
                StreamWriter sw = new StreamWriter(szLogPathFile, true);

                //Write a line of text
                foreach (string strLog in listLog)
                {
                    sw.WriteLine(curDT.ToString() + " " + strLog);
                }
                //Close the file
                sw.Close();
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

		// Check last write date of log file and Delete Expired Log Files
		public void DeleteExpiredFile()
		{
			// Iterate through folder
			Directory.CreateDirectory(m_szLogPath);
			DirectoryInfo dir = new DirectoryInfo(m_szLogPath);
			foreach (FileInfo f in dir.GetFiles("*." + m_szLogExt)) 
			{
				if (CheckFileExpired(f))
				{
					try
					{
						f.Delete();
					}
					catch (Exception ex)
					{
                        throw ex;
					}
				}
			}
		}

		// check if file expired
		// parameter:
		//	fd - use FindFirstFile and FindNextFile to get WIN32_FIND_DATA 
		//		 data type
		public bool CheckFileExpired(FileInfo f)
		{
			DateTime fileTime = f.LastWriteTime;
			DateTime curDT = DateTime.Today;

			TimeSpan dayDiff = curDT - f.LastWriteTime.Date;

			if (dayDiff.Days > m_nExpireDays || f.Length > m_lSize)
			{
				return true;
			}
			return false;
		}

	}
}
